$(function() {
	/*
	 * $('#saveLogo').click(function(){ var logo = $("#logoButton").attr("src");
	 * var _error = $(this).siblings('.error_txt'); if(logoButton == '' ||
	 * logoButton == null){ _error.html('请选择logo').show();
	 * setTimeout(function(){_error.html('').hide();}, 3000); return false;
	 * }else{ $.ajax({ type:"post", url:"/user/accountSettings",
	 * data:{"type":1,"logo":logo}, dataType: "json", success:function(data){
	 * if(data.status==true){ alert("修改logo成功"); location.reload(); } else {
	 * alert("修改logo失败"); } } }) } });
	 */

	$('#saveOrgName').click(function() {
		var _orgName = removeAllSpace($('#orgName').val());
		var _error = $(this).siblings('.error_txt');
		if (_orgName == '' || _orgName == null) {
			_error.html('请输入名称').show();
			setTimeout(function() {
				_error.html('').hide();
			}, 3000);
			return false;
		} else {
			$.ajax({
				type : "post",
				url : "/user/accountSettings",
				data : {
					"type" : 2,
					"userName" : _orgName
				},
				dataType : "json",
				success : function(data) {
					if (data.status == true) {
						// alert("修改名称成功");
						location.reload();
					} else {
						// alert("修改名称失败");
					}
				}
			})
		}
	});
	$('#saveOrgPassword').click(function() {
		var oldPassword = removeAllSpace($('#oldPassword').val());
		var newPassword = removeAllSpace($('#newPassword').val());
		var confirmPassword = removeAllSpace($('#confirmPassword').val());
		var _error = $(this).siblings('.error_pass_txt');

		if (oldPassword == '' || oldPassword == null) {
			_error.html('请输入旧密码').show();
			setTimeout(function() {
				_error.html('').hide();
			}, 3000);
			return false;
		} else if (newPassword == '' || newPassword == null) {
			_error.html('请输入新密码').show();
			setTimeout(function() {
				_error.html('').hide();
			}, 3000);
			return false;
		} else if (confirmPassword == '' || confirmPassword == null) {
			_error.html('请再次输入新密码').show();
			setTimeout(function() {
				_error.html('').hide();
			}, 3000);
			return false;
		} else if (newPassword != confirmPassword) {
			_error.html('两次密码不一致').show();
			setTimeout(function() {
				_error.html('').hide();
			}, 3000);
			return false;
		} else {
			$.ajax({
				type : "post",
				url : "/user/accountSettings",
				data : {
					"type" : 3,
					"oldPassword" : oldPassword,
					"newPassword" : newPassword,
					"confirmPassword" : confirmPassword
				},
				dataType : "json",
				success : function(data) {
					if (data.status == true) {
						// alert("修改密码成功");
						location.reload();
					} else {
						// alert("修改密码失败");
					}
				}
			})
		}
	});

	$('#saveOrgPhone').click(function() {
		var newPhone = removeAllSpace($('#newPhone').val());
		var phoneCode = removeAllSpace($('#phoneCode').val());
		var _error = $(this).siblings('.error_txt');
		if (newPhone == '' || newPhone == null) {
			_error.html('请输入手机号码').show();
			setTimeout(function() {
				_error.html('').hide();
			}, 3000);
			return false;
		} else if (phoneCode == '' || phoneCode == null) {
			_error.html('请输入手机验证码').show();
			setTimeout(function() {
				_error.html('').hide();
			}, 3000);
			return false;
		} else {
			$.ajax({
				type : "post",
				url : "/user/accountSettings",
				data : {
					"type" : 4,
					"phone" : newPhone,
					"phoneCode" : phoneCode
				},
				dataType : "json",
				success : function(data) {
					if (data.status == true) {
						// alert("修改手机成功");
						location.reload();
					} else {
						// alert("修改手机失败");
					}
				}
			})
		}
	});
	$('#saveOrgEmail').click(function() {
		var newEmail = removeAllSpace($('#newEmail').val());
		var emailCode = removeAllSpace($('#emailCode').val());
		var _error = $(this).siblings('.error_txt');
		if (newEmail == '' || newEmail == null) {
			_error.html('请输入邮箱地址').show();
			setTimeout(function() {
				_error.html('').hide();
			}, 3000);
			return false;
		} else if (emailCode == '' || emailCode == null) {
			_error.html('请输入邮箱验证码').show();
			setTimeout(function() {
				_error.html('').hide();
			}, 3000);
			return false;
		} else {
			$.ajax({
				type : "post",
				url : "/user/accountSettings",
				data : {
					"type" : 4,
					"email" : newEmail,
					"emailCode" : emailCode
				},
				dataType : "json",
				success : function(data) {
					if (data.status == true) {
						// alert("修改邮箱地址成功");
						location.reload();
					} else {
						// alert("修改邮箱地址失败");
					}
				}
			})
		}
	});
	$('.confirm_unbindWechat').click(function() {
		$.ajax({
			type : "post",
			url : "/user/accountSettings",
			data : {
				"type" : 6,
				"openId" : ''
			},
			dataType : "json",
			success : function(data) {
				if (data.status == true) {
					alert("解绑微信成功");
					location.reload();
				} else {
					alert("解绑微信失败");
				}
			}
		})
	});
})

function removeAllSpace(str) {
	return str.replace(/\s+/g, "");
}

$(".sendEmailCode").click(function() {
	var newEmail = removeAllSpace($('#newEmail').val());
	var _error = $(this).siblings('.error_txt');
	if (newEmail == '' || newEmail == null) {
		_error.html('请输入邮箱地址').show();
		setTimeout(function() {
			_error.html('').hide();
		}, 3000);
		return false;
	}
	$.ajax({
		type : "post",
		url : "/user/sendEmailCode",
		data : {
			"email" : newEmail
		},
		dataType : "json",
		success : function(data) {
			if (data.status == true) {
				alert(data.data);
			} else {
				alert(data.data);
			}
		}
	})
})

$("#getCaptcha").click(function() {

	countdown = getCookieValue("secondsremained");
	if (countdown > 0) {
		showTips("发送验证短信间隔少于1分钟");
		return false;
	}
	var phone = $("#newPhone").val();
	// var captcha = $("#code").val();

	var regMobile = /^1[3|4|5|6|7|8|9][0-9]{1}[0-9]{8}$/;
	if (phone == '' || phone == null || phone == '手机') {
		showTips("手机号码不能为空");
		return false;
	}

	if (!regMobile.test(phone)) {
		showTips("手机格式错误");
		return false;
	}

	// if(captcha == '' || captcha == null){
	// showTips("图形验证码不能为空");
	// return false;
	// }

	$.ajax({
		type : "post",
		url : "/user/sendCaptchaByPhone",
		data : {
			"phone" : phone
		},
		dataType : "json",
		success : function(data) {
			if (data.status == true) {
				showTips("验证码已发送到手机!");
				curCount = count;
				// 设置button效果，开始计时
				$("#getCaptcha").attr("disabled", "disabled");
				$("#phoneCode").val(data.captchaCode);
				addCookie("secondsremained", 60, 60);// 添加cookie记录,有效时间60s
				SetRemainTime();// 开始倒计时
			} else {
				showTips(data.msg);
			}
		}
	});
})

// 发送验证码时添加cookie
function addCookie(name, value, expiresHours) {
	var cookieString = name + "=" + escape(value);
	// 判断是否设置过期时间,0代表关闭浏览器时失效
	if (expiresHours > 0) {
		var date = new Date();
		date.setTime(date.getTime() + expiresHours * 1000);
		cookieString = cookieString + ";expires=" + date.toUTCString();
	}
	document.cookie = cookieString;
}

var InterValObj; // timer变量，控制时间
var count = 60; // 间隔函数，1秒执行
var curCount;// 当前剩余秒数
var countdown;

function SetRemainTime(phone) {
	countdown = getCookieValue("secondsremained");
	if (countdown == 0) {
		$("#getCaptcha").removeAttr("disabled");// 启用按钮
		$("#getCaptcha").html("重新发送验证码");
		$("#getCaptcha").css("background-color", "#ec584e");
	} else {
		countdown--;
		$("#getCaptcha").html(countdown + "秒后再发送");
		$("#getCaptcha").attr("disabled", "disabled");
		$("#getCaptcha").css("background-color", "#666");
		editCookie("secondsremained", countdown, countdown + 1);
		setTimeout(function() {
			SetRemainTime()
		}, 1000) // 每1000毫秒执行一次
	}
}

// 修改cookie的值
function editCookie(name, value, expiresHours) {
	var cookieString = name + "=" + escape(value);
	if (expiresHours > 0) {
		var date = new Date();
		date.setTime(date.getTime() + expiresHours * 1000); // 单位是毫秒
		cookieString = cookieString + ";expires=" + date.toGMTString();
	}
	document.cookie = cookieString;
}
// 根据名字获取cookie的值
function getCookieValue(name) {
	var strCookie = document.cookie;
	var arrCookie = strCookie.split(";");
	for (var i = 0; i < arrCookie.length; i++) {
		var arr = arrCookie[i].split("=");
		if (Trim(arr[0]) == name) {
			return unescape(arr[1]);
			break;
		}
	}
	return "";

}

function Trim(str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

/* 信息提示框 */
function showTips(str) {
	var doc = document;

	// 如果提示框已被创建，则只改变文本内容，否则创建提示框
	if (doc.querySelector(".tips") !== null) {
		doc.querySelector(".tips span").innerHTML = str;
		doc.querySelector(".tips").style.display = "block";
	} else {
		var body = doc.querySelector("body"), tips = doc.createElement("div"), span = doc
				.createElement("span");

		// 设置提示框样式，并将节点插入到body中
		tips.style.cssText = "position:fixed; top:0;z-index:9999; width:100%; height:100%; background-color:rgba(0,0,0,.5);color:red;";
		span.style.cssText = "max-width:60%;padding:30px 50px;border-radius:2px;background-color:#fff;position:relative;top:50%;transform:translateY(-50%);margin-left:auto;margin-right:auto;box-shadow:0 0 20px #000; display:inline-block; text-align:left;";
		span.innerHTML = str;
		tips.appendChild(span);
		tips.className = "tips text-center";
		body.appendChild(tips);

		// 点击时隐藏提示框
		tips.onclick = function() {
			this.style.display = "none";
		};
	}
	;

};

function saveLogo(logo) {
	$.ajax({
		type : "post",
		url : "/user/accountSettings",
		data : {
			"type" : 1,
			"logo" : logo
		},
		dataType : "json",
		success : function(data) {
			if (data.status == true) {
				alert("修改logo成功");
				location.reload();
			} else {
				alert("修改logo失败");
			}
		}
	})
}

function uploadLogo() {
	/** ******上传头像******* */
	var clickId = "logoButton"; // 上传按钮id
	var fileName = "imageFile"; // 文件对应服务器名字
	var url = '/uploadFile/img';
	var maxFileSize = '5mb'; // 文件最大尺寸
	var FileType = 'jpg,gif,png,jpeg'; // 文件类型
	var progressBar = ''; // 文件上传进度条
	var successType = 'img'; // 文件处理类型判断，主要用于区分裁剪功能
	var showImgDiv = '#logoButton,.current-pic img' // 文件上传后，显示在哪个DIV
	uploadFile(fileName, url, clickId, maxFileSize, FileType, progressBar,
			successType, showImgDiv); // 上传图片
}
jQuery(function($){
	
	var cutPicData={};

	$(".current-pic>img").cropper({
	    aspectRatio: 1 / 1,
	    preview:'.preview',
	    minCropBoxWidth:150,
	    minCropBoxHeight:150,
	    crop: function(e) {
	    	//获取图片裁剪参数
	    	cutPicData.x=parseInt(e.x);
		    cutPicData.y=parseInt(e.y);
		    cutPicData.width=parseInt(e.width);
		    cutPicData.height=parseInt(e.height);
		    cutPicData.src=$(".current-pic>img").attr('src');
	    }
	}); 
	
	//保存裁剪
	$(".save-cut-logo").click(function(){
		console.log(cutPicData); 
		$.post(
			"/uploadFile/cutLogo",
			{
				"x":cutPicData.x,
				"y":cutPicData.y,
				"width":cutPicData.width,
				"height":cutPicData.height,
				"url":cutPicData.src
			},
			function(data){
				if(data.status==true){
					$("#logoButton").attr("src",data.data);
					$("#logoButton").attr("value",data.data);
					saveLogo(data.data);
				}
			},"json"
		);
		
		
		//关闭裁剪框
		$("#cutpic-modal").hide();
	});
	
	$(".cancel-upload-logo").click(function(){
		$("#cutpic-modal").hide();
	});

});
